var mongoose = require('mongoose')

var PictureSchema = new mongoose.Schema({
	title: String,
	poster: String,
	summary: String,
	author: String,
	meta: {
		createAt:{
			type: Date,
			defaule: Date.now()
		},
		updateAt:{
			type: Date,
			defaule: Date.now()
		}
	}
})

PictureSchema.pre('save', function(next){
	if(this.isNew) {
		this.meta.createAt = this.meta.updateAt = Date.now()
	}
	else {
		this.meta.updateAt = Date.now()
	}
	next()
})

PictureSchema.statics={
	fetch: function(cd){
		return this
			.find({})
			.sort('meta.updateAt')
			.exec(cd)
	},
	findById: function(id, cd){
		return this
			.findOne({_id: id})
			.sort('meta.updateAt')
			.exec(cd)
	}
}
module.exports = PictureSchema